﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace IMLibrary.GZip
{
    public class Stream
    {
        /// <summary>
        /// 压缩流
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static byte[] Compress(byte[] Data)
        {
            if (Data == null || Data.Length == 0)
                return Data;
            using (MemoryStream ms = new MemoryStream())
            {
                GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);

                Compress.Write(Data, 0, Data.Length);

                Compress.Close();

                return ms.ToArray();

            }
        }
        /// <summary>
        /// 解压缩流
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static byte[] Decompress(Byte[] Data)
        {
            if (Data == null || Data.Length == 0)
                return Data;
            using (MemoryStream tempMs = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(Data))
                {
                    GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress);

                    Decompress.CopyTo(tempMs);

                    Decompress.Close();

                    return tempMs.ToArray();
                }
            }
        }

    }
}
